package com.guoliang.statisticsService.schedule;

import com.guoliang.statisticsService.service.CourseClient;
import com.guoliang.viewcount.utils.ViewCountRedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.xml.soap.SAAJResult;
import java.util.Set;

/**
 * @author ：Leo
 * @date ：Created in 2023/2/5 13:42
 * @description：每日凌晨进行浏览量统计
 */
@Component
public class ViewCountScheduled {
    @Autowired
    ViewCountRedisUtils redisUtils;
    @Autowired
    CourseClient courseClient;

    /**
     * 每天0点30分 获取当天存储的所有key进行viewcount计算添加
     */
    @Scheduled(cron = "0 30 0 * * ? *")
    public void addViewCount(){
        Set<String> keys = redisUtils.getKeys("course*");
        for(String key:keys){
            Long viewCount = redisUtils.size(key);
            String id=key.substring(key.indexOf(':')+1);
            courseClient.updateViewCount(id,viewCount);
            redisUtils.delete(key);
        }
    }
}
